<template>
  <div>
    <div>
      <FormPanel ref="form" :list="formList" :cols="3" :formType="type" :isSubmitBtn="false" :labelWidth="130" @formChange="changeHandle" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeHandler">关 闭</el-button>
      <!-- <AjaxButton type="primary" :click="selectActiveHandle" :disabled="daisabledMember">选择活动会员</AjaxButton> -->
      <AjaxButton :click="saveHandler" type="primary" :disabled="daisabledSave"> <i class="iconfont icon-baocun"></i>保 存 </AjaxButton>
    </div>
  </div>
</template>

<script>
import moment from 'moment';
import { saveOutOrder } from '@/api/part/partOther/prizeOutbound';
import { getStorageList, getAllUser } from '@/api/part/common';
import { notifyAction, camelcase, jsonToHump } from '@/utils/index.js';
import { queryPartStock } from '@/api/part/partOutbound/repairMaterials/index.js';
import Cookies from 'js-cookie';
export default {
  name: 'AddPrizeOutbound',
  props: { storageList: { type: Array }, outOrderEmit: { type: Object }, clickType: { type: String } },
  data() {
    return {
      formList: this.createFormlist(),
      type: 'add',
      daisabledSave: false,
      saveReturnData: {},
      formDataList: {}, // 表单信息
      partData: {} // 配件信息
    };
  },
  created() {},
  mounted() {
    this.checkAssignType();
  },
  methods: {
    // 赋值操作判断
    checkAssignType() {
      // console.log(this.clickType, '点击类型');

      if (this.clickType == 'edit') {
        this.assignForm();
      }
    },
    // 编辑赋值
    assignForm() {
      // 赋值
      this.formList.forEach(element => {
        let val;
        try {
          val = this.outOrderEmit[element.fieldName];
        } catch (error) {}
        if (!val) {
          return;
        }
        element.initialValue = val;
      });
      this.partData = this.outOrderEmit;
    },
    // 关闭操作
    closeHandler(param) {
      this.$emit('close', false, param);
    },
    // 保存信息
    async saveInfo(val) {
      let res = await saveOutOrder(val);
      if (res.resultCode == 200) {
        this.saveReturnData.id = res.data;
        return notifyAction('保存成功', 'success');
        // return this.$notify.success({ title: '提示信息', message: '保存成功' });
      }
    },
    // 查询配件库存
    async getPartStock() {
      let [err, formData] = await this.$refs.form.GET_FORM_DATA();
      if (err) {
        return;
      }
      this.formDataList = formData;
      let res = await queryPartStock(this.formDataList);
      if (res.resultCode == 200) {
        if (res.data.rows.length < 1 || res.data.rows[0]['USEABLE_STOCK'] < 1) {
          return notifyAction('该仓库此配件库存不足', 'error');
        }
        // 判断可用数量
        notifyAction('查询配件库存信息成功', 'success');
        jsonToHump(res.data.rows[0]);
        this.partData = res.data.rows[0];
        // 校验库位
        if (!this.partData.storagePositionCode) {
          return notifyAction('该仓库此配件库位为空', 'error');
        }
        // 赋值
        this.formList.forEach(element => {
          let val;
          if (element.fieldName == 'createdAt') {
            return;
          }
          try {
            val = res.data.rows[0][element.fieldName];
          } catch (error) {}
          if (!val) {
            return;
          }
          element.initialValue = val;
        });
      }
    },
    changeHandle(val) {
      this.formDataList = val;
    },
    //
    async getFormData() {
      let [err, formData] = await this.$refs.form.GET_FORM_DATA();
      if (err) {
        return;
      }
      this.formDataList = formData;
    },
    // 保存操作
    async saveHandler() {
      let [err, formData] = await this.$refs.form.GET_FORM_DATA();
      if (err) {
        return;
      }
      // 测试数据
      // let saveData = {
      //   awardOutNo: '',
      //   awardNo: 'CS0001',
      //   awardCode: '0124',
      //   receiptDate: '2020-02-02',
      //   customerCode: 'test009',
      //   customerName: '哇塞',
      //   partNo: '000 001 000',
      //   unit: '001',
      //   costPrice: 29,
      //   costAmount: 58,
      //   outPrice: 30,
      //   outAmount: 0.0,
      //   outQuantity: 2,
      //   storageCode: '01',
      //   storagePositionCode: 'A_1265',
      //   remark: ''
      // };
      // 执行保存
      formData.costPrice = this.partData.costPrice;
      formData.outPrice = this.partData.costPrice;
      formData.costAmount = formData.costPrice * formData.outQuantity;
      formData.outAmount = formData.outPrice * formData.outQuantity;
      formData.storagePositionCode = this.partData.storagePositionCode;
      await this.saveInfo(formData);
      this.$emit('saveHandle', this.saveReturnData);
    },
    createFormlist() {
      return [
        {
          type: 'INPUT',
          label: '出库单号',
          fieldName: 'awardOutNo',
          disabled: true,
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '兑奖单',
          fieldName: 'awardNo',
          placeholder: '请输入...',
          rules: [{ required: true, message: '兑奖单不能为空', trigger: 'blur' }]
        },
        {
          type: 'DATE',
          label: '申请日期',
          fieldName: 'receiptDate',
          // disabled: true,
          initialValue: moment().format('YYYY-MM-DD HH:mm:ss'),
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '客户代码',
          fieldName: 'customerCode',
          // disabled: true,
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '客户名称',
          // disabled: true,
          fieldName: 'customerName',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '兑奖码',
          disabled: true,
          fieldName: 'awardCode',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '配件代码',
          // disabled: true,
          fieldName: 'partNo',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '配件名称',
          // disabled: true,
          fieldName: 'partName',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT_NUMBER',
          // disabled: true,
          label: '待兑奖数量',
          fieldName: 'outQuantity',
          placeholder: '请输入...',
          rules: [{ required: true, message: '请输入待兑奖数量', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '仓库',
          fieldName: 'storageCode',
          placeholder: '请输入...',
          itemList: this.storageList,
          change: this.getPartStock
        },
        {
          type: 'INPUT',
          label: '批号',
          disabled: true,
          fieldName: 'batchNo',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '可用数量',
          disabled: true,
          fieldName: 'useableStock',
          placeholder: '请输入...'
        },
        {
          type: 'DATE',
          label: '保质截止日期',
          disabled: true,
          fieldName: 'shelfDays',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '备注',
          disabled: true,
          fieldName: 'remark',
          selfCols: 2,
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '操作员代码',
          fieldName: 'operator',
          disabled: true,
          initialValue: Cookies.get('userId'),
          placeholder: '请输入...'
        },
        {
          type: 'DATE',
          label: '创建日期',
          disabled: true,
          initialValue: moment().format('YYYY-MM-DD HH:mm:ss'),
          fieldName: 'createdAt',
          placeholder: '请输入...'
        }
      ];
    }
  }
};
</script>

<style></style>
